package org.hcf;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: zuyong.xu
 * @date: 2022/1/10
 */
//@Component
public class UploadFileHandle {

    public String uploadFile(File file) {
        String url = null;
        byte[] bytes = fileToBytes(file);

        Map<String, Object> params = new LinkedHashMap<>();
        params.put("bytes", bytes);
        try {
            //请求外部接口要放在try语句中

        } catch (Exception e) {
            System.out.println("上传请求外部接口异常");
        }
        return url;
    }


    /**
     * 将File文件转成byte数组
     *
     * @param file
     * @return
     */
    public static byte[] fileToBytes(File file) {
        byte[] buffer = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int n;
            while ((n = fis.read(bytes)) != -1) {
                bos.write(bytes, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}
